import React, { Component } from 'react';
import { Modal } from 'antd';
import $$ from 'cmn-utils';
import config from '@/config';

interface BaseComponentProps {
  history: any;
}

interface BaseComponentState {
  record: object | null;
  visible: boolean;
}

class BaseComponent extends Component<BaseComponentProps, BaseComponentState> {
  notice: string = config.notice;

  get history() { 
    return this.props.history;
  }

  onAdd = (): void => {
    this.setState({
      record: null,
      visible: true
    });
  };

  onUpdate = (record: object): void => {
    this.setState({
      record,
      visible: true
    });
  };

  onDelete = (record: object | object[]): void => {
    if (!record) return;
    if ($$.isArray(record) && !record.length) return;

    const content = `您是否要删除这${
      $$.isArray(record) ? record.length : ''
    }项？`;

    Modal.confirm({
      title: '注意',
      content,
      onOk: () => {
        this.handleDelete($$.isArray(record) ? record : [record]);
      },
      onCancel() {}
    });
  };

  handleAdd(): void {
    /* 子类重写 */
  }
  
  handleUpdate(): void {
    /* 子类重写 */
  }
  
  handleDelete(records: object[]): void {
    /* 子类重写 */
  }
}

export default BaseComponent;